<?php if(!defined('BASEPATH')) exit('No script direct allowed access');

class Member_model extends CI_Model
{
	private $table = '';
	
	public function __construct()
	{
		parent::__construct();
		
		$this->table = 'member';
	}
	
	public function add($data)
	{
		if($this->db->insert($this->table, $data)) return TRUE; else return FALSE;
	}
	
	public function checkUser($user)
	{
		$sql = $this->db->select('username')->get_where($this->table, array('username' => $user));
		
		if($sql->num_rows() > 0 )
		{
			return TRUE;
		} else {
		 	return FALSE;
		}
	}

	public function get_name($id) {
		$sql = $this->db->select('fullname')->get_where($this->table, array('id' => $id))->row();
		if($sql->fullname !="" ) {
			return $sql->fullname;
		} else {
			return "";
		}
	}
	
	public function checkEmail($email)
	{
		$sql = $this->db->select('email')->get_where($this->table, array('email' => $email));
		
		if($sql->num_rows() > 0 )
		{
			return TRUE;
		} else {
		 	return FALSE;
		}
	}

	public function getListMember($where = null, $order= null, $limit = null) {
		if($where !== null) {
			foreach($where as $key => $value) {
				$this->db->where($key, $value);
			}
		} 
		if($order !== null) {
			foreach($order as $key => $value) {
				$this->db->order_by($key, $value);
			}
		}
		if($limit !== null) {
			$this->db->limit($limit['max'], $limit['begin']);
		}

		$sql = $this->db->get($this->table);
		return $sql;
		free_result($sql);
	}

	public function delete($id) {
		//Delete all comment from comment table
		$this->db->delete('comment', array('memId' => $id));

		//Delete all comments of this member from comment_photo table
		$this->db->delete('comment_photo', array('memId' => $id));

		//Delete all item of this member from like_comment
		$this->db->delete('like_comment', array('memId' => $id));

		//Delete from like_video of this member
		$this->db->delete('like_video', array('memId' => $id));

		//Delete from respected_person
		$this->db->delete('respected_for_people', array('memId' => $id));

		//Deelte from member table
		$avatar = $this->db->select('avatar')->get_where('member', array('id' => $id))->row();
		if($avatar->avatar !="") @unlink($avatar->avatar);
		$this->db->delete('member', array('id' => $id));
		return TRUE;
	}
}
